/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import com.ad.common.utils.FileUtils;
import com.ad.common.utils.StringUtils;


/**
 * This example demonstrates the use of the {@link ResponseHandler} to simplify
 * the process of processing the HTTP response and releasing associated resources.
 */
public class ClientWithResponseHandler {//                                                                                                                         微博地址									头像地址
	/**
	 * 从新浪微博解析信息的正则表达式
	 * 1:微博地址
	 * 2：微博名称
	 * 3：头像地址
	 * 4：null
	 * 5：性别
	 * 6：地址 北京，东城区
	 * 7：关注
	 * 8：粉丝
	 * 9：微博数
	 * 10:
	 * 11:简介1
	 * 12:
	 * 13:标签
	 */
	private static String patternStr = "<div\\s*?class=\"list_person clearfix\">\\s*?" +
	"<div\\s*?class=\"person_pic\">\\s*?<a.*?href=\"(.*?)\".*?title=\"(.*?)\".*?>[\\s]*?<img.*?src=\"(.*?)\".*?>[\\s]*?</a>[\\s]*?</div>[\\s]*?" +
			"(<div\\s*?class=\"person_adbtn\">[\\s\\S]*?</div>){0,1}\\s*?" +
			"<div\\s*?class=\"person_detail\">\\s*?" +
			"<p\\s*?class=\"person_name\">[\\s\\S]*?</p>\\s*?" +
			"<p\\s*?class=\"person_addr\">\\s*?<span\\s*?class=\".*?m_icon\"\\s*?title=\"(.*?)\">\\s*?</span>\\s*?<span\\s*?>(.*?)</span>[\\s\\S]*?</p>[\\s\\S]*?" +
			"<p\\s*?class=\"person_num\">\\s*?" +
			"<span>[\\s\\S]*?<a.*?>\\s*?([\\d\\D]*?)\\s*?</a>\\s*?</span>\\s*?" +//关注
			"<span>[\\s\\S]*?<a.*?>\\s*?([\\d\\D]*?)\\s*?</a>\\s*?</span>\\s*?" + 	//粉丝
			"<span>[\\s\\S]*?<a.*?>\\s*?([\\d\\D]*?)\\s*?</a>\\s*?</span>\\s*?</p>[\\s\\S]*?" +
			"(<div\\s*?class=\"person_info\">[\\s]*?<p>([\\s\\S]*?)</p>[\\s]*?</div>[\\s\\S]*?){0,1}" +
			"(<p\\s*?class=\"person_label\">([\\s\\S]*?)</p>){0,1}[\\s\\S]*?" +
			"</div>\\s*?" +
			"</div>";		//微博标签：
	/**
	 * 1:头像地址
	 * 2、名称
	 * 3、
	 * 4、广播
	 * 5、广播数
	 * 6、
	 * 7、听众
	 * 8、听众数
	 * 9、
	 * 10、他收听
	 * 11、他收听数
	 * 12、
	 * 13、收录他
	 * 14、收录他的数
	 */
	private static String patternQQ = "<li[\\s\\S]*?class=\"pic\">[\\s\\S]*?<a[\\s\\S]*?>[\\s\\S]*?<img[\\s\\S]*?src=\"(.*?)\"[\\s\\S]*?</a>[\\s\\S]*?</li>[\\s\\S]*?" +
			"<li\\s*?class=\"detail\">[\\s\\S]*?class=\"userName\">(.*?)</span>[\\s\\S]*?class=\"userNums\">[\\s\\S]*?" +
			"(<a.*?>\\s*?(.*?)\\s*?<strong[\\s\\S]*?>(\\d*?)</strong>[\\s\\S]*?</a>[\\s\\S]*?)?" +
			"(<a.*?>\\s*?(.*?)?\\s*?<strong[\\s\\S]*?>(\\d*?)</strong>[\\s\\S]*?</a>[\\s\\S]*?)?" +
			"(<a.*?>\\s*?(.*?)?\\s*?<strong[\\s\\S]*?>(\\d*?)</strong>[\\s\\S]*?</a>[\\s\\S]*?)?" +
			"(<a.*?>\\s*?(.*?)?\\s*?<strong[\\s\\S]*?>(\\d*?)</strong>[\\s\\S]*?</a>[\\s\\S]*?)?" +
			"</div>[\\s\\S]*?</li>" ;
	/**
	 * 企业信息获取的正则
	 * 1、头像地址
	 * 2、昵称
	 * 3、听众
	 * 4、听众数
	 * 5、收听
	 * 6、收听数
	 * 7、广播
	 * 8、广播数
	 */
	private static String patternQQCompany = "<div\\s*?class=\"m_profile co_m_profile clear\">[\\s\\S]*?" +
				"<div\\s*?class=\"lc\">[\\s\\S]*?<img[\\s\\S]*?src=\"(\\S*?)\"[\\s\\S]*?</div>[\\s\\S]*?" +
				"<div\\s*?class=\"rc\">[\\s\\S]*?class=\"text_user\">(.*)</span>[\\s\\S]*?</div>[\\s\\S]*?" +
				"<div\\s*?class=\"co_m_profile_data clear co_bgr3\">[\\s\\S]*?" +
				"<ul[\\s\\S]*?" +
				"<li[\\s\\S]*?span[\\s\\S]*?>([\\s\\S]*?)</span>[\\s\\S]*?<a[\\s\\S]*?>([\\s\\S]*?)</a>[\\s\\S]*?</li>[\\s\\S]*?" +
				"<li[\\s\\S]*?span[\\s\\S]*?>([\\s\\S]*?)</span>[\\s\\S]*?<a[\\s\\S]*?>([\\s\\S]*?)</a>[\\s\\S]*?</li>[\\s\\S]*?" +
				"<li[\\s\\S]*?span[\\s\\S]*?>([\\s\\S]*?)</span>[\\s\\S]*?<a[\\s\\S]*?>([\\s\\S]*?)</a>[\\s\\S]*?</li>[\\s\\S]*?" +
				"</ul>[\\s\\S]*?" +
				"</div>[\\s\\S]*?" +
			"</div>";
//	private static String patternStr = "\\\\n" ;
	
	
//	private static String patternStr = "<div\\s*?class=\\\\\"list_person clearfix\\\\\">\\s*?<div\\s*?class=\\\\\"person_pic\\\\\">\\s*?<a[\\s\\S]*?href=\\\\\"(.*)\\\\\"[\\s\\S]*?>\\s*?<img[\\s\\S]*?src=\\\\\"(.*?)\\\\\".*?>\\s*?</a>" ;
	
    public static void main(String[] args) throws Exception {
    	testQQ();
//    	testSina();
    // testInLocal();
    }
    public static void testSina(){
       Pattern pattern = Pattern.compile(patternStr);
 	   String SEARCH_URL_SINA = "http://s.weibo.com/user/&nickname=${queryStr}&Refer=SUer_box";
        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet httpget = new HttpGet(SEARCH_URL_SINA.replace("${queryStr}", URLEncoder.encode("天山傲雪","UTF-8")));

            System.out.println("executing request " + httpget.getURI());
            
            // Create a response handler
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
//        	File file = new File("D:\\test.htm");
//        	String responseBody = getFileContent(file) ;
            System.out.println("&&&&"+responseBody);
        	responseBody = responseBody.replaceAll("\\\\\"", "\"");
        	responseBody = responseBody.replaceAll("\\\\/", "/");
        	responseBody = responseBody.replaceAll("\\\\t", "");
        	responseBody = responseBody.replaceAll("\\\\n", "");
        	
            System.out.println("----------------------------------------");
          
            System.out.println(responseBody);
            test(responseBody,pattern,"测试：");
//            Matcher matcher = pattern.matcher(responseBody);
//            while(matcher.find()){
//            	System.out.println("&&&&"+matcher.group());
//            }
//            System.out.println("111----------------------------------------");

        } catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
//            httpclient.getConnectionManager().shutdown();
        }
 }
    public static void testQQ(){
//    	Pattern pattern = Pattern.compile(patternQQ);
    	Pattern pattern = Pattern.compile(patternQQCompany);
    	
    	String SEARCH_URL_QQ = "http://t.qq.com/${queryStr}";
        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet httpget = new HttpGet(SEARCH_URL_QQ.replace("${queryStr}", URLEncoder.encode("baidu","UTF-8")));

            System.out.println("executing request " + httpget.getURI());
            
            // Create a response handler
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
            System.out.println("=========================================");
//            System.out.println(responseBody);
        	responseBody = responseBody.replaceAll("\\\\\"", "\"");
        	responseBody = responseBody.replaceAll("\\\\/", "/");
        	responseBody = responseBody.replaceAll("\\\\t", "");
        	responseBody = responseBody.replaceAll("\\\\n", "");
        	
            System.out.println("----------------------------------------");
          
            test(responseBody,pattern,"测试：");
//            Matcher matcher = pattern.matcher(responseBody);
//            while(matcher.find()){
//            	System.out.println("&&&&"+matcher.group());
//            }
//            System.out.println("111----------------------------------------");

        } catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
           httpclient.getConnectionManager().shutdown();
        }
 }
    public static void testInLocal(){
    	File file = new File("D:\\test.htm");
    	String responseBody = getFileContent(file) ;
    	responseBody = responseBody.replaceAll("\\\\\"", "\"");
    	responseBody = responseBody.replaceAll("\\\\/", "/");
    	responseBody = responseBody.replaceAll("\\\\t", "");
    	responseBody = responseBody.replaceAll("\\\\n", "");
    	
    	
    	
        System.out.println("----------------------------------------");
      
        System.out.println(responseBody);
//        test(responseBody,pattern,"测试：");
    }
    public static String getFileContent(File file) {
		String encoding = "UTF-8";
		try {
			encoding = FileUtils.getCharset(file);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		String content = "";
		StringBuffer sb = new StringBuffer();
		InputStreamReader isr = null;
		BufferedReader br = null;
		String tmpStr = "";
		try {
			isr = new InputStreamReader(new FileInputStream(file), encoding);
			br = new BufferedReader(isr);
			while ((tmpStr = br.readLine()) != null) {
				sb.append(tmpStr);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (isr != null)
					isr.close();
				if (br != null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		content = sb.toString();
		if(!encoding.equals("UTF-8")){
			try {
				content = new String(content.getBytes("UTF-8"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return content;
	}
    public static void test(String content, Pattern p, String tag) {
		Matcher m = p.matcher(content);
			int index = 0;
			while (m.find()) {
				index ++ ;
				int size = m.groupCount();
				System.out.println("m.group0=="+m.group(0));
				if (size > 0) {
					String tmp ;
					for (int i = 1; i <= size; i++) {
						tmp = m.group(i);
						System.out.println(tag+index+" i="+i+"="+tmp);
						if(tmp!=null)
						System.out.println("=="+StringUtils.decodeUnicode(tmp));
					
						
					}
				}
			}
	}

}

